package com.einmeer.sercuritydemo.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 芊嵛
 * @date 2024/3/1
 */
//用这个配置体现不出thymeleaf的作用，可以在application中配置
//@RequestMapping("/demo")
@RestController
public class IndexController {
    @GetMapping("/")
    public Map index() {
        SecurityContext context = SecurityContextHolder.getContext();

        Authentication authentication = context.getAuthentication();
        Object principal = authentication.getPrincipal(); // 获取用户信息
        Object credentials = authentication.getCredentials(); // 获取用户凭证信息
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); // 获取用户权限信息

        String name = authentication.getName();
        HashMap result = new HashMap();
        result.put("username",name);
        result.put("authorities",authorities);
        return result;
    }
}
